package network.message;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 * Created by IntelliJ IDEA.
 * User: grigory.rozhkov
 * Date: 11.09.12
 * Time: 23:29
 * To change this template use File | Settings | File Templates.
 */
public class SocketMessageReceiver implements IMessageReceiver {

    private Socket connection;

    public SocketMessageReceiver(Socket connection) {
        this.connection = connection;
    }

    public String receiveMessage() {
        BufferedInputStream bufferedInputStream = null;
            //Trying to create input stream on socket:
            try {
                bufferedInputStream = new BufferedInputStream(connection.getInputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
            //Defining input stream:
            InputStreamReader inputStreamReader = new InputStreamReader(bufferedInputStream);

            StringBuilder message = new StringBuilder();
            try {
                int c;
                while ((c = inputStreamReader.read()) != 13) {
                    message.append((char) c);
                }
            } catch (IOException e) {
                e.printStackTrace();
                //TODO: smth useful
            }
            return message.toString();
    }
}
